﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Clipper.Model;
using System.Threading;
using Global;
using System.Windows;
using System.Collections.ObjectModel;

using Communication;
using Clipper.Model.ViewModel;
using static Clipper.Model.DataBaseModel.DisplayCup;

namespace Clipper.BLL.DetectChain
{
    /// <summary>
    /// 孵育
    /// </summary>
    public class IncubationBox : ApplianceComponent 
    {
        #region 变量
        /// <summary>
        /// 孵育床摇晃控制
        /// </summary>
        public static BedControl BedControl = new BedControl();
        /// <summary>
        /// 孵育控制
        /// </summary>
        public static IncubationControl IncubationControl = new IncubationControl();
        /// <summary>
        /// 孵育箱出下一个反应条倒计时
        /// </summary>
        public static System.Timers.Timer mTimerCountDown = new System.Timers.Timer();
        public static DateTime mdtStart;
        public static bool mIsCDRunning = false;//孵育箱倒计时是否启动
        #endregion

        #region 属性
        static ControlStatusEnum detectionStatus;
        static bool mIsPause;
        static ObservableCollection<byte> seatList = null;
        static ObservableCollection<double> mTemperature;
        //static ObservableCollection<int> mTempDetect;

        /// <summary>
        /// 状态
        /// </summary>
        public override ControlStatusEnum DetectionStatus
        {
            get
            {
                if (ServiceProvider.Monitor.IncubationBoxReactionStrips.Count >= ServiceProvider.MaxIncubatorCount)//到达最大数量
                {
                    detectionStatus = ControlStatusEnum.BUSY;
                }
                else
                {
                    detectionStatus = ControlStatusEnum.IDLE;
                }
                return detectionStatus;
            }
            set
            {
                detectionStatus = value;
            }
        }
        static AutoResetEvent mPause = new AutoResetEvent(false);//暂停

        /// <summary>
        /// 获得或设置模块暂停状态
        /// </summary>
        public static bool IsPause
        {
            get
            {
                return mIsPause;
            }
        }
        /// <summary>
        /// 空位置列表
        /// </summary>
        public static ObservableCollection<byte> SeatList
        {
            get
            {
                if (seatList == null)
                {
                    seatList = new ObservableCollection<byte>();

                    byte _position = ServiceProvider.MaxIncubatorCount;//硬件位置
                    for (byte i = 1; i <= ServiceProvider.MaxIncubatorCount; i++)
                    {
                        seatList.Add(_position);
                        _position--;
                    }
                }
                return seatList;
            }
            set
            {
                seatList = value;
            }
        }

        /// <summary>
        /// 孵育箱温度集合，四个温度
        /// </summary>
        public static ObservableCollection<double> Temperature
        {
            get
            {
                if (mTemperature == null)
                {
                    mTemperature = new ObservableCollection<double>();
                }
                return mTemperature;
            }
            set
            {
                mTemperature = value;
            }
        }

        //检测位温度显示
        //public static ObservableCollection<int> TempDetect
        //{
        //    get
        //    {
        //        if (mTempDetect==null)
        //        {
        //            mTempDetect = new ObservableCollection<int>();
        //        }
        //        return mTempDetect;
        //    }
        //    set
        //    {
        //        mTempDetect = value;
        //    }
        //}

        public static double TemperatureAVG
        {
            get
            {
                double total = 0;
                foreach (double _temp in Temperature)
                {
                    total += _temp;
                }
                return total / 4;
            }
        }


        #endregion

        public IncubationBox()
        {
        }

        public override void Run(ReactionCup cup)
        {
            if (ServiceProvider.Monitor.DisplayCup[cup.CupSeatID].getStatus() == CheckCancelEnum.CANCEL)
            {
                //ServiceProvider.Monitor.DisplayCup[cup.CupSeatID].setStatus(CheckCancelEnum.NOTCAN);

                //ServiceProvider.CupStrip[cup.StripID].Cups[cup.CupSeatID].setStatus(Model.DetectionModel.Cup.CupHoleStatusEnum.END);
                //ServiceProvider.Monitor.DisplayCup[cup.StripID * 4 + cup.CupSeatID].CupStatus = "Cancel";
                //return;
                
            }
            else
            {

                int Incubation_delay = 0;

                ServiceProvider.Monitor.DisplayCup[cup.StripID * 4 + cup.CupSeatID].CupStatus = "孵育";

                //设定的延时时间
                if (ServiceProvider.CupStrip[cup.StripID].Cups[cup.CupSeatID].getStatus() == Model.DetectionModel.Cup.CupHoleStatusEnum.SAMPLE)
                {
                    //刚加完样本的延时时间
                    Console.WriteLine("样本加完后延时时间：" + cup.DetectItemModel.incubationTime.ToString() + "\r");

                    Incubation_delay = cup.DetectItemModel.incubationTime;
                    //Thread.Sleep(cup.DetectItemModel.incubationTime * 1000);

                }
                else if (ServiceProvider.CupStrip[cup.StripID].Cups[cup.CupSeatID].getStatus() == Model.DetectionModel.Cup.CupHoleStatusEnum.REAGENT1)
                {
                    //刚加完试剂2的延时时间
                    Console.WriteLine("试剂2加完后延时时间：" + cup.DetectItemModel.incubationTime1.ToString() + "\r");

                    Incubation_delay = cup.DetectItemModel.incubationTime1;
                    //Thread.Sleep(cup.DetectItemModel.incubationTime1 * 1000);
                }
                else if (ServiceProvider.CupStrip[cup.StripID].Cups[cup.CupSeatID].getStatus() == Model.DetectionModel.Cup.CupHoleStatusEnum.REAGENT2)
                {
                    Console.WriteLine("试剂3加完后延时时间：" + cup.DetectItemModel.incubationTime2.ToString() + "\r");
                    //加完试剂3的延时时间
                    Incubation_delay = cup.DetectItemModel.incubationTime2;
                    // Thread.Sleep(cup.DetectItemModel.incubationTime2 *  1000);
                }

                while (Incubation_delay > 0)
                {

                    ServiceProvider.Monitor.DisplayCup[cup.StripID * 4 + cup.CupSeatID].CupStatus = "孵育..." + Incubation_delay.ToString();

                    Thread.Sleep(1000);
                    Incubation_delay--;
                    if (ServiceProvider.Monitor.DisplayCup[cup.StripID * 4 + cup.CupSeatID].getStatus() == CheckCancelEnum.CANCEL)
                    {
                        break;
                        //ServiceProvider.Monitor.DisplayCup[cup.CupSeatID].setStatus(CheckCancelEnum.NOTCAN);

                        //ServiceProvider.CupStrip[cup.StripID].Cups[cup.CupSeatID].setStatus(Model.DetectionModel.Cup.CupHoleStatusEnum.END);
                        //ServiceProvider.Monitor.DisplayCup[cup.StripID * 4 + cup.CupSeatID].CupStatus = "Cancel";
                        //return;
                    }

                }

               


                //strip.ReactionStripSeat = ReactionStripStageEnum.INCUBATIONBOX;//改变位置
                //strip.Note = this.GetType().Name + Thread.CurrentThread.Name + "---" + strip.ID;
                //Console.WriteLine(strip.Note);

                //if (ServiceProvider.IsSimulate)
                //{
                //    Simulate(strip);
                //}
                //else
                //{
                //    //strip.IncubatorEnd = DateTime.Now.AddMinutes(strip.WhileIncubator);//实际用
                //    if (strip.DetectionMethod == DetectionMethodEnum.ONE_STEP_METHOD)
                //    {
                //        strip.IncubatorEnd = DateTime.Now.AddMinutes(40);
                //    }
                //    else//两步法的孵育
                //    {
                //        if (strip.FirstIncubation)
                //        {
                //            strip.IncubatorEnd = DateTime.Now.AddMinutes(3);
                //            strip.FirstIncubation = false;
                //        }
                //        else
                //        {
                //            strip.IncubatorEnd = DateTime.Now.AddMinutes(2);
                //        }
                //    }
                //    if (!mIsCDRunning)
                //    {
                //        IncubationBox.mTimerCountDown.Start();
                //        mIsCDRunning = true;
                //    }
                //    TimeSpan timeSpan =  strip.IncubatorEnd -DateTime.Now;
                //    Thread.Sleep(timeSpan);     //持续时间孵育

                //}
                ////if (strip.ID <= 5)
                ////{
                //    Transportation.AddTransportation(strip, this, NextComponent);
                ////}
            }
            //执行下一条链

            NextComponent.Run(cup);
        }

        /// <summary>
        /// 断电后的重新运行
        /// </summary>
        public void ReCoverRun()
        {

        }
        /// <summary>
        /// 暂停/恢复
        /// </summary>
        /// <param name="isPause">暂停true,恢复false</param>
        public static void Pause(bool isPause)
        {
            mIsPause = isPause;
            IncubationControl.IsPause = isPause;
        }
        public override void Simulate(ReactionCup strip)
        {
            //strip.IncubatorEnd = DateTime.Now.AddMinutes(strip.WhileIncubator);
            //int sleep = int.Parse((strip.WhileIncubator * 1000 * ServiceProvider.ForwardTimes).ToString());
            //Thread.Sleep(sleep);     //持续时间孵育
        }
        /// <summary>
        /// 工程师调试方法
        /// </summary>
        /// <param name="strip">反应条</param>
        public static void RunEngineer(ReactionStrip strip)
        {
            Transportation.IncubationTransport.GetStrip(10);
            Transportation.IncubationTransport.WaitStatus(11);
            Transportation.IncubationTransport.DropStrip(20);
            Transportation.IncubationTransport.WaitStatus(10);

        }
    }
}
